/*****************************************************************************
 *
 *                      HUSONG PROPRIETARY INFORMATION
 *
 *          The information contained herein is proprietary to HUSONG
 *           and shall not be reproduced or disclosed in whole or in part
 *                    or used for any design or manufacture
 *              without direct written authorization from HUSONG.
 *
 *            Copyright (c) 2015 by HUSONG.  All rights reserved.
 *
 *****************************************************************************/
package me.send.produce.data.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import me.send.business.base.Pager;
import me.send.data.service.pojo.City;
import me.send.data.service.pojo.CityBean;
import me.send.data.service.pojo.base.DatatablesViewPage;
import me.send.produce.data.controller.common.CommonController;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author maxmin
 *
 * @date 2015年9月29日 下午3:35:42
 */
@Controller
@RequestMapping(value="/city")
public class OpencityController extends CommonController{
	
	/**
	 * 开放城市列表
	 * @return
	 * @author maxmin
	 * @date 2015年10月8日上午10:56:10
	 */
	@RequestMapping(value="/list")
	public ModelAndView openCityList(){
		ModelAndView modelAndView = new ModelAndView("");
		List<City> list = cityService.getCitiesList();
		List<City> openCitiesList = cityService.getOpenCitiesList();
		modelAndView.addObject("openCities", openCitiesList);
		modelAndView.addObject("cities", list);
		return modelAndView;
	}
	
	
	// 查询提现订单列表
		@RequestMapping(value = "orders/list")
		public ModelAndView withdraw_list(HttpServletRequest request) {
			ModelAndView modelAndView = new ModelAndView(
					"stage/orders/city_list");
			modelAndView.addObject("LoadTree",
					treeService.main_tree(0, request, "city/list"));
			modelAndView.addObject("module", "开放城市列表");
			return modelAndView;
		}

		
		@RequestMapping(value="page", method=RequestMethod.GET)  
	    @ResponseBody  
	    public DatatablesViewPage<CityBean> datatablesTest(HttpServletRequest request){  
	//获取分页控件的信息  
	        String start = request.getParameter("start");  
	        String length = request.getParameter("length"); 
	//获取前台额外传递过来的查询条件  
	        SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm");
	        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        String start_time = request.getParameter("start_time"); 
	        String end_time = request.getParameter("end_time");  
	        String name = request.getParameter("name");  
	        String letter = request.getParameter("letter");  
	        String status = request.getParameter("status");  
	        Integer paramStatus=null;
	        if(status==null||(status!=null&&"".equals(status.trim()))){
	        	status=null;
	        }else {
	        	paramStatus=Integer.parseInt(status);
	        }
	        if(name==null||(name!=null&&"".equals(name.trim()))){
	        	name=null;
	        }
	        if(letter==null||(letter!=null&&"".equals(letter.trim()))){
	        	letter=null;
	        }
	        if(start_time!=null&&!"".equals(start_time.trim())&&end_time!=null&&!"".equals(end_time.trim())){
	        	Date startDate=null;
	        	Date endDate=null;
	        	try {
	        		startDate=sdf.parse(start_time);
	        		start_time=format.format(startDate);
	        		endDate=sdf.parse(end_time);
	        		end_time=format.format(endDate);
	        	} catch (ParseException e) {
	        		// TODO Auto-generated catch block
	        		e.printStackTrace();
	        	}
	        }else{
	        	start_time=null;
	        	end_time=null;
	        }
	        
	        Pager page=new Pager();
	        Integer row=Integer.parseInt(length);
	        page.setRows(row);
	        Integer startInt=Integer.parseInt(start);
	        Integer p=(startInt/row)+1;
	        page.setPage(p);
	        
	                //随便组织的查询结果  
	        List<CityBean> list=cityService.list(name, letter, paramStatus, start_time, end_time, page); 
	        Integer count=cityService.count(name, letter, paramStatus, start_time, end_time);
	        DatatablesViewPage<CityBean> view = new DatatablesViewPage<CityBean>();  
	        view.setiTotalDisplayRecords(count);  
	        view.setiTotalRecords(count);  
	          
	        view.setAaData(list);  
	        return view;  
	    }
		
		
		/**
		 * 开放
		 * @param opencityid
		 * @return
		 */
		@RequestMapping(value = "open")
		@ResponseBody
		public void open(int opencityid) {
			cityService.updateStatus(opencityid, 1);
		}
		/**
		 * 关闭
		 * @param opencityid
		 * @return
		 */
		@RequestMapping(value = "close")
		@ResponseBody
		public void close(int opencityid) {
			cityService.updateStatus(opencityid, 0);
		}
}
